<?php

class AppController extends Controller
{
	public $helpers = array('Session', 'Html', 'Form', 'Menu', 'Minify');

	public $components = array('RequestHandler', 'Session', 'Auth');

	public $uses = array('User');

	protected $_allowActions = array();
	protected $_denyActions = array();

	public function beforeFilter()
	{
		$this->set('title_for_layout', __('For your healthy brain', true));
		$this->set('baseUrl', Configure::read('Web.baseUrl'));

		// set system title
		$isSystem = $this->getParam('system', false);
		if($isSystem){
			Utils::loadModels($this, array('SystemUser'));
			$this->layout = 'system_default';
			$this->Auth->sessionKey = 'Auth.SystemUser';
			$this->Auth->userModel = 'SystemUser';
			$this->Auth->authenticate = $this->SystemUser;
			$this->Auth->loginAction = array('system' => true, 'plugin' => null, 'controller' => 'users', 'action' => 'login');
			$this->Auth->loginRedirect = '/system/content/content_posts';
		} else {
			$this->Auth->loginAction = array('plugin' => null, 'controller' => 'users', 'action' => 'login');
			$this->Auth->allow('*');
			$this->Auth->sessionKey = 'Auth.User';
			$this->Auth->authenticate = $this->User;
			$this->Auth->userScope = array('User.status' => User::STATUS_ACTIVE);
		}

		if($this->isAjax()){
			$this->layout = 'ajax';
		}

		// auth configuration
		$this->Auth->fields = array('username' => 'email', 'password' => 'password');
		$this->Auth->autoRedirect = false;
		$this->Auth->allow($this->_allowActions);
		$this->Auth->deny($this->_denyActions);
		$this->Auth->authError = 'Bạn phải xác thực thông tin cá nhân trước khi sử dụng tính năng này.';
		$this->Auth->loginError = 'Thông tin bạn gửi không đúng, xin vui lòng kiểm tra email và password thử lại.';

		// base params
		parent::beforeFilter();
	}

	protected function getParam($key, $default = null){
		if(empty($key)){
			throw new Exception('$key should not be empty');
		}
		$keys = explode('.', $key);
		$params = $this->params;
		foreach($keys as $k){
			if(isset($params[$k])){
				$params = $params[$k];
			} else {
				return $default;
			}
		}
		return $params;
	}

	protected function isPost()
	{
		return $this->RequestHandler->isPost();
	}

	protected function isAjax(){
		return $this->RequestHandler->isAjax();
	}

	protected function sendAjax($type = 'JSON'){
		 $this->autoRender=false;
		 switch ($type){
		 	case 'JSON' :
		 		$this->RequestHandler->setContent('json', 'text/x-json');
		 		$this->output = json_encode(!empty($this->response) ? $this->response : $this->viewVars);
		 	break;
		 	default:
		 		$this->RequestHandler->setContent('text', 'text/html');
		 		$this->render();
		 }
	}

	/*
	 * Show info message
	 * $params: 'class' should be: 'error', 'success', 'warn' or 'info'
	 * */
	protected function setFlash($messages, $params = array('class' => 'error'), $key = 'flash', $element = 'flash_message') {
		$msg = is_array($messages) ? implode( "<br/>", array_values( $messages ) ) : $messages;
		$this->Session->setFlash( $msg, $element, $params, $key );
	}
}